package org.springcloud.plugin.framework.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * author: yanggld
 * date: 2019/12/21 15:42
 * version: 1.0
 * description:
 * 上下文初始化启动器，其实现类需配置在spring.factories
 * */
public abstract class PluginApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        configurableApplicationContext.getBeanFactory().addBeanPostProcessor(new BeanPostProcessor(){
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                return afterInitialization(configurableApplicationContext, bean, beanName);
            }
        });

    }

    protected abstract Object afterInitialization(ConfigurableApplicationContext configurableApplicationContext, Object bean, String beanName);
}
